
package br.edu.sistemaacademico;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import br.edu.sistemaacademico.facade.DataFacade;
import br.edu.sistemaacademico.pojo.Classe;
import br.edu.sistemaacademico.pojo.Disciplina;
import br.edu.sistemaacademico.pojo.Professor;
import br.edu.sistemaacademico.pojo.Servidor;
import br.edu.sistemaacademico.pojo.Turma;
import br.edu.sistemaacademico.state.StateHandler;
import client.ClientCommand;

public class frmServidor extends FrmModelo{

	StateHandler stateAlterarExcuir = new StateHandler(false);
    public List<Object> campos = ClientCommand.getCampos("TurmaService");//new ArrayList<Object>();
    private List<Object> valores = new ArrayList<Object>();
    private List<Object> tamanho = new ArrayList<Object>();
    FrmModelo frmA;
    Servidor servidor ;
    List<Turma> extra;
    TabelaModelo tabModel;
    
    public frmServidor(java.awt.Frame parent, boolean modal,Servidor servidor) {
         super(parent, modal);
         this.servidor = servidor;
         tamanho.add("50");
         setTitle("Servidor");
         tabModel = new TabelaModelo(campos, tamanho, getJTable());
         tabModel.busca(getTfBuscar());
         setButtonAction(getJbtnGravar(), "disciplina");
         setButtonAction(getbtnAlterar(), "curso");
         setButtonAction(getbtnExcluir(), "aluno");
         setButtonAction(getbtn4(), "professor");
         setButtonAction(getbtn5(), "servidor");
         setButtonAction(getbtn6(), "turma");
         setButtonAction(getbtnVoltar(), "voltar");
         setButtons();
         //atualiza();
         setVisible(true);
    }

    
    public void setButtonAction(JButton btn, String op) {
        if(op.equals("servidor")){
        btn.addActionListener(listener);
        btn.setActionCommand("atualiza");}
        else if(op.equals("aluno")){
            btn.addActionListener(listener);
            btn.setActionCommand("aluno");
        }else if(op.equals("professor")){
            btn.addActionListener(listener);
            btn.setActionCommand("professor");
        }else if(op.equals("disciplina")){
            btn.addActionListener(listener);
            btn.setActionCommand("disciplina");
        }else if(op.equals("curso")){
            btn.addActionListener(listener);
            btn.setActionCommand("curso");
        }else if(op.equals("voltar")){
            btn.addActionListener(listener);
            btn.setActionCommand("voltar");
        }else if(op.equals("turma")){
            btn.addActionListener(listener);
            btn.setActionCommand("turma");
        }
        //btn.addActionListener(ps);
    }

    public void setButtons(){
        JButton btn1 = getJbtnGravar();
        JButton btn2 = getbtnAlterar();
        JButton btn3 = getbtnExcluir();
        JButton btn4 = getbtn4();
        JButton btn5 = getbtn5();
        JButton btn6 = getbtn6();
        btn1.setText("Disciplina");
        btn2.setText("Curso");
        btn3.setText("Aluno");
        btn4.setText("Professor");
        btn5.setText("Servidor");
        btn6.setText("Turma");
        setNone();
        //getJTable().setVisible(false);
    }
    public void seila(){

    }
    ActionListener listener = new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            if (ae.getActionCommand().equals("aluno"))
                frmA = new frmAlunoServidor(new Frame(), true);
            else if(ae.getActionCommand().equals("professor"))
                frmA = new frmProfessorServidor(new Frame(), true);
            else if(ae.getActionCommand().equals("disciplina"))
                frmA = new frmDisciplina(new Frame(), true);
            else if(ae.getActionCommand().equals("curso"))
                frmA = new frmCurso(new Frame(), true);
            else if(ae.getActionCommand().equals("voltar")){
            	dispose();
			}else if(ae.getActionCommand().equals("turma"))
                frmA = new frmTurma(new Frame(), true);
			else if(ae.getActionCommand().equals("atualiza"))
                new frmAtualizaServidor(servidor);
            
        }
    };
    public void atualiza(){
		((DefaultTableModel) getJTable().getModel()).setNumRows(0);
		getJTable().updateUI();
		this.extra = (List) ClientCommand.carregar("TurmaService","codigo=codigo", "");
		DataFacade entrada = new DataFacade();   
    	for(int i = 0; i < extra.size();i++ ){
    		valores = new LinkedList<Object>();
    		valores.add(extra.get(i).getId());
    		valores.add(extra.get(i).getTamanho());
    		valores.add(extra.get(i).getPeriodo());
    		extra.get(i).setProfessor((Professor) ClientCommand.carregaObjeto("ProfessorService"," codigo ="+extra.get(i).getProfessor().getId(),""));
			extra.get(i).setClasse((Classe) ClientCommand.carregaObjeto("ClasseService","codigo ="+extra.get(i).getClasse().getId(),""));
			extra.get(i).getClasse().setDisicplina((Disciplina) ClientCommand.carregaObjeto("DisciplinaService","codigo ="+extra.get(i).getClasse().getDisicplina().getId(),""));
    		valores.add(extra.get(i).getProfessor().getNome());
    		valores.add(extra.get(i).getClasse().getDisicplina().getNome());
    		tabModel.setValores(valores);
    		valores.clear();
    		valores.removeAll(extra);
    	}
	}
}